//
//  AddressViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/25.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AddressViewController.h"
#import "AddressCell.h"
#import "AddressInputViewController.h"

@interface AddressViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 新增地址Btn
 */
@property (nonatomic , strong) UIButton *addAddressBtn;
@end

@implementation AddressViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地址管理";
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //数据请求
    [self initData];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //[self.navigationController closeScreenPopGesture];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //[self.navigationController openScreenPopGesture];
}
-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    [self.dataArr removeAllObjects];
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    [[[PigMarketApi sharedInstance] getAddressListWithMemberId:memberId] subscribeNext:^(AddressList * _Nullable addressList) {
        [self.dataArr addObjectsFromArray:addressList.list];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 110;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
    
    UIButton *addAddressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addAddressBtn setTitle:@"新增地址" forState:UIControlStateNormal];
    [addAddressBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    [addAddressBtn addTarget:self action:@selector(addAddressTapped) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:addAddressBtn];
    [addAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(48);
    }];
    return footerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressCell *cell = [AddressCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        cell.addressModel = self.dataArr[indexPath.row];
    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressInputViewController *addressInputVC = [[AddressInputViewController alloc] init];
    addressInputVC.title = ModifyAddressTitle;
    Address *addresModel = self.dataArr[indexPath.row];
    addressInputVC.addressModel = addresModel;
    [self.navigationController pushViewController:addressInputVC animated:YES];

}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    @weakify(self);
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        @strongify(self);
        [self deleteAddressRequestWithIndex:indexPath.row];
    }];
    return @[deleteAction];
}


#pragma mark - target action
//新增地址
- (void)addAddressTapped {
    AddressInputViewController *addressInputVC = [[AddressInputViewController alloc] init];
    addressInputVC.title = AddAddressTitle;
    [self.navigationController pushViewController:addressInputVC animated:YES];
}
#pragma mark - private method
//删除地址请求
- (void)deleteAddressRequestWithIndex:(NSInteger)index {
    Address *addressModel = self.dataArr[index];
    NSString *receiveAddressId = addressModel.receiveAddressId;
    [[[PigMarketApi sharedInstance] deleteAddressWithReceiveAddressId:receiveAddressId] subscribeNext:^(NSNull * _Nullable x) {
        
        [self initData];
    } error:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
//        _myTableView.rowHeight = 90;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
